--
-- Author: shunguo.chen
-- Date: 2017-06-28 12:47:35
--
-- SettingView

local TabsBtn = require("app.widget.TabsBtn")
local ListViewExtend = require("app.widget.ListViewExtend")

local SettingBasePanel = import(".SettingBasePanel")
local SettingBattlePanel = import(".SettingBattlePanel")
local SettingIMPanel = import(".SettingIMPanel")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.SettingScene

function ClassRef:init()
	-- panel pos
	self._panelPos = cc.p(self._ui.Node_pos:getPosition())
	self._ui.Node_pos:removeFromParent()

	-- btnList
	self._btnList = self._ui.ListView
	self._btnList:setItemModel(self._btnList:getItem(0))
	ListViewExtend.scrollToItemExtend(self._btnList)

	for i=2, #self._data do
		self._btnList:pushBackDefaultItem()
	end
	
	local items = self._btnList:getItems()
	for i,item in ipairs(items) do
		local data = self._data[i]
		local nameText = item:getChildByName("name")
		nameText:setString(data.name)
		nameText:setTextColor(cc.c3b(109,109,109))
	end

	self._binds = {}
    local function onTabsBtn(index, btn)
    	if self._curIdx then
	    	self._curIdx = index
	    	self:switchPanel()
	    else
	    	self._curIdx = index
	    end
	    self._btnList:scrollToItem(btn)
    end
    local function onSelectBtn(btn, b)
    	btn:getChildByName("name"):setTextColor(b and cc.c3b(255,225,132) or cc.c3b(109,109,109))
    end
    self._tabsBtn = TabsBtn.new(items, onTabsBtn, nil, self._binds, onSelectBtn)

    -- btn 
    self._ui.Button_close:addClickEventListener(function()
    		executeFunc(self._onCloseView)
		end)
	-- 客服按钮
	self._ui.Button_service:addClickEventListener(function()
		app.channelAPI:openURL("https://kf.qq.com/touch/scene_product.html?scene_id=kf6455")
	end)
	if app:getInnerPublisher() == Enums.Publisher.tencent then
		self._ui.Button_service:setVisible(true)
	else
		self._ui.Button_service:setVisible(false)
	end
	-- 用户ID
	if cc.isAlive(self._ui.id) then
		self._ui.id:setString(string.format(L("userIDstring"), UD.player.uid))
	end
end

function ClassRef:show(index)
	self.super.show(self)
	self:switchPanel(index)
end

function ClassRef:onSwitchPanel(cb)
	self._onSwitchPanel = cb
end

function ClassRef:getPanel(idx)
	return self._binds[idx]
end

function ClassRef:switchPanel(index)
	if index then
		self._curIdx = index
		self._tabsBtn:setSelected(index,true)
	end
	self:createPanel(self._curIdx)
	executeFunc(self._onSwitchPanel, self._curIdx)
end

function ClassRef:createPanel(index)
	if not self._binds[index] then
		local panel
		if index == 1 then
			panel = SettingBasePanel.create()
		elseif index == 2 then
			panel = SettingBattlePanel.create()
		elseif index == 3 then
			panel = SettingIMPanel.create()
		end
		panel:setPosition(self._panelPos)
		self._ui.Panel:addChild(panel)
		self._binds[index] = panel
	end
end


return ClassRef